package com.ys.zero.web;

import java.io.IOException;





import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.internet.AddressException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.ys.zero.bean.Concerned;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.Uarticle;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.biz.UserBiz;
import com.ys.zero.dao.UserDao;
import com.ys.zero.dao.articalDao;
import com.ys.zero.util.EmailUtils;
import com.ys.zero.util.VerifyRound;


@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private UserDao  dao = new UserDao();
	private articalDao adao = new articalDao();
	private UserBiz biz = new UserBiz();

	/**
	 * 请求地址 /user/login.s
	 */
	// 于 doGet 除了名字不同， 其他（参数，返回值类型。。。）必须都一样
	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String account = request.getParameter("account");
		String pwd = request.getParameter("pwd");
		try {
			User user = biz.login(account, pwd);
			
			request.getSession().setAttribute("loginedUser", user);
			toJson(response, new Result(1, "登录成功",user.getRole()));
		} catch (BizException e) {
			e.printStackTrace();
			toJson(response, new Result(0, e.getMessage()));
		}
	}

	protected void regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			try {
				response.setHeader("cache-control", "no-cache");
				// 获取注册者邮箱
				String mail = request.getParameter("mail");
				String name = request.getParameter("name");
				//获取注册者的密码passwrod
				String password = request.getParameter("passwrod");
				String password1 = request.getParameter("passwrod1");
				//获取验证码
				String code = request.getParameter("code");
				if(mail.isEmpty() || mail.trim().isEmpty()) {
					toJson(response, new Result(0, "邮箱不能为空"));
					return;
				}
				if(password.isEmpty() || password1.isEmpty() 
						|| password.trim().isEmpty()||password1.trim().isEmpty()) {
					toJson(response, new Result(0, "密码不能为空"));
					return;
				}
				if(password.equals(password1)==false) {
					toJson(response, new Result(0, "密码不一致"));
					return;
				}
				// 获取会话对象
				HttpSession session = request.getSession();
				String verify1 = (String) session.getAttribute("verify1");
				if(verify1.equals(code)==false) {
					toJson(response, new Result(0, "验证码错误"));
					return;
				}
				// 向数据库提交用户注册信息
				String account = biz.insertRegist(password,name,mail);
				toJson(response, new Result(1,account));
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	public void sendEmail(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String mail = request.getParameter("mail");
		String code = request.getParameter("code");
		//判断邮箱是否为空
		if(mail==null) {
			toJson(response, new Result(0, "邮箱为空"));
			return;
		}
		try {
			//发送邮件
			String verify1 = biz.sendEmail1(mail);
			//存入会话
			HttpSession session = request.getSession();
			session.setAttribute("verify1",verify1);
			toJson(response, new Result(1, "成功"));
		} catch (BizException e) {
			toJson(response, new Result(0,e.getMessage()));
		}
	}

	
	public void getBack(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String account = request.getParameter("account");
		try {
			User user = biz.getBack(account);
			//发一封验证码
			String verify = biz.sendEmail(account);
			ServletContext content = getServletContext();
			content.setAttribute("verify",verify);
			content.setAttribute("account",account);
			toJson(response,new Result(1,"成功"));
			/*
			 * String content = VerifyRound.getRound();
			 * EmailUtils.sendMail(user.getEmail(),"找回密码验证码",content);
			 */
		} catch (BizException e) {
			toJson(response,new Result(0,e.getMessage()));
			e.printStackTrace();
		}
		
	}
	
	public void insertNewPwd(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String pwd1 = request.getParameter("pwd1");
		String pwd2 = request.getParameter("pwd2");
		try {
			ServletContext content = getServletContext();
			biz.insertNewPwd(pwd1,pwd2,content.getAttribute("account"));
			toJson(response, new Result(1,"修改成功"));
		} catch (BizException e) {
			toJson(response, new Result(0,e.getMessage()));
			e.printStackTrace();
		}
	}
	
	public void showFollow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		List<Concerned> list = dao.showFollow(id);
		 List<User> list1 = new ArrayList<User>();   
		 List<User> list2 = new ArrayList<User>();		
		 for (int i = 0; i < list.size(); i++) {
			 list1.add(i,dao.getAll(list.get(i).getBaccountId()).get(0));
			 list2.add(i,list1.get(i));
			 }
		toJson(response, list2);
	}
	/***
	 * 判断是否关注
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void isGz(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		List<Concerned> list = dao.showFollow(id);
		toJson(response, list);
	}

	
	
	/**
	 * 取消关注
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	public void DtGz(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uid = request.getParameter("uid");
		int uaid =     Integer.parseInt(uid);
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		dao.DeleteGz(uaid, id);
	}
	public void Gz(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uid = request.getParameter("uid");
		int uaid =     Integer.parseInt(uid);
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		dao.InsertGz(uaid, id);
	}
	/**
	 * 后台的粉丝等信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void UGetFence(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		User user = (User) request.getSession().getAttribute("loginedUser");
		Integer id = user.getId();
		  Object  list1 =  dao.getFence(id);
		  List<Map<String,Object>>  list2 =  adao.getInfo(id); 
		  List<Object> listAll = new ArrayList<Object>();
		  listAll.add(list1);
		  listAll.add(list2);
		  toJson(response, listAll);
	}
	/**
	 * 得到名片
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void GetMp(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		 String a =   request.getParameter("id");
		int id = Integer.parseInt(a);
		List<User> list =    	dao.getAll(id);
		toJson(response, list);
	}
	/**
	 * 名片页面的点赞数量等
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void Getinfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		 String a =   request.getParameter("id");
		int id = Integer.parseInt(a);
		 Object  list1 =  dao.getFence(id);
		  List<Map<String,Object>>  list2 =  adao.getInfo(id); 
		  List<Object> listAll = new ArrayList<Object>();
		  listAll.add(list1);
		  listAll.add(list2);
		  toJson(response, listAll);
	}
	/**
	 * 名片页面的关注
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void MpshowFollow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ids  = request.getParameter("id");
		int id = Integer.parseInt(ids);
		
		List<Concerned> list = dao.showFollow(id);
		 List<User> list1 = new ArrayList<User>();   
		 List<User> list2 = new ArrayList<User>();		
		 for (int i = 0; i < list.size(); i++) {
			 list1.add(i,dao.getAll(list.get(i).getBaccountId()).get(0));
			 list2.add(i,list1.get(i));
			 }
		toJson(response, list2);
	}
	
	
	/**
	 * 名片页面的粉丝
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void MyFence(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ids  = request.getParameter("id");
		int id = Integer.parseInt(ids);
		
		List<Concerned> list = dao.getMpfs(id);
		 List<User> list1 = new ArrayList<User>();   
		 List<User> list2 = new ArrayList<User>();		
		 for (int i = 0; i < list.size();  i++) {
			 list1.add(i,dao.getAll(list.get(i).getQaccountId()).get(0));
			 list2.add(i,list1.get(i));
			 }
		toJson(response, list2);
	}
}
